<?php

/*
 * @File:框架文件 
 * @E-mail:lei@congshan.net
 */


//自PHP5.4起系统默认关闭自动转义，CongPHP将设定为所有的版本都不转义。
if (function_exists("get_magic_quotes_gpc")) {

    if (get_magic_quotes_gpc()) {

        function stripslashes_gpc($value) {
            if (is_array($value)) {
                $value = array_map('stripslashes_gpc', $value);
            } else {
                $value = stripslashes($value);
            }
            return $value;
        }

        $_POST = array_map('stripslashes_gpc', $_POST);
        $_GET = array_map('stripslashes_gpc', $_GET);
        $_COOKIE = array_map('stripslashes_gpc', $_COOKIE);
        $_REQUEST = array_map('stripslashes_gpc', $_REQUEST);
    }
}
/*
 * CongPHP框架类
 */

use framework\system\kernel\ExceptionEx;
use framework\system\manager\ComponentManager;
use framework\system\manager\MessageManager;
use framework\system\manager\EventManager;

class framework {

    protected static $componentManager;
    protected static $messageManager;
    protected static $eventManager;
    /*
     * 保存CLI模式下传入的参数
     * @static Array
     */
    public static $argv = [];
    public static $cli = false;

    public static function main($argv) {

        static::$componentManager = new ComponentManager;
        static::$messageManager = new MessageManager;
        static::$eventManager = new EventManager;
        if (!empty($argv)) {
            /*
             * 保存参数
             */
            static::$argv = $argv;
            static::$cli = TRUE;
        }



        //echo spl_autoload_functions();
        /*
         * 加载核心组件
         */
        static::cm()->init();
        static::em()->init();
        static::mm()->init();
        do {
            /*
             * 设定PHP超时
             */
            set_time_limit(FRAMEWORK_TIME_LIMIT);
            /*
             * 执行事件
             */
            static::em()->erun();
            /*
             * 等待
             */
            usleep(MIRCO_TIME_SLEEP);
        } while (!static::mm()->get("message.framework.exit"));
    }

    public static function cm() {

        return static::$componentManager;
    }

    public static function em() {

        return static::$eventManager;
    }

    public static function mm() {

        return static::$messageManager;
    }

    public static function componentManager() {

        return static::$componentManager;
    }

    public static function eventManager() {

        return static::$eventManager;
    }

    public static function messageManager() {

        return static::$messageManager;
    }

    public static function getOption($string = NULL) {

        $return = NULL;
        foreach (static::$argv as $value) {
            if ($string == $value) {
                $return = current(static::$argv);
            }
        }
        return $return;
    }

    public static function shutdownHandler() {
        $GLOBALS['time_CongPHP_end'] = microtime(true);
        $GLOBALS['time_CongPHP_use'] = sprintf("%0.5f", $GLOBALS['time_CongPHP_end'] - $GLOBALS['time_CongPHP_start']);
        if (isset($_GET["time_use"])) {
            echo "<!--time_CongPHP_use_" . $GLOBALS['time_CongPHP_use'] . "-->";
        }
    }

    public static function errorHandler($errno, $errstr, $errfile, $errline) {

        ExceptionEx::errorHandler($errno, $errstr, $errfile, $errline);
    }

    public static function exceptionHandler($e) {
        ExceptionEx::exceptionHandler($e);
    }

}
